<template>
    <div v-if="visibleComputed" class="sdialog-wrapper" @click="closeModal">
        <div class="wrap" :style="{ width, height, background: bg }" @click.stop>
            <i v-if="showCloseIcon" class="close-icon" @click.stop="close"></i>
            <div class="content">
                <slot></slot>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import { computed, defineEmits } from "vue";

const emit = defineEmits(["update:modelValue", "close"]);

const props = defineProps({
    width: String,
    height: String,
    closeOnClickModal: Boolean,
    beforeClose: Function,
    modelValue: Boolean,
    showCloseIcon: {
        type: Boolean,
        default() {
            return true;
        },
    },
    bg: {
        type: String,
        default: "#fff",
    },
});

const visibleComputed = computed({
    get() {
        return props.modelValue;
    },
    set(val) {
        emit("update:modelValue", val);
    },
});

const close_pop = () => {
    emit("update:modelValue", false);
    emit("close");
};

const close = () => {
    const close_f = close_pop;
    if (props.beforeClose) {
        props.beforeClose(close_f);
    } else {
        close_f();
    }
};

const closeModal = () => {
    if (props.closeOnClickModal) {
        close();
    }
};
</script>
<style lang="scss" scoped>
.sdialog-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 9999;
    background: rgb(0, 0, 0, 0.7);
    .wrap {
        position: absolute;
        top: 50%;
        left: 50%;
        width: 80%;
        overflow-y: auto;
        transform: translate(-50%, -50%);
        border-radius: 10rpx;
        background: #fff;
        .content {
            width: 100%;
            height: 100%;
            min-height: 100rpx;
        }
    }
}
</style>
